package com.learn.project2.service.wx.comment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.*;
import com.learn.project2.mapper.MarketCommentMapper;
import com.learn.project2.mapper.MarketUserMapper;
import com.learn.project2.mapper.wx.comment.CommentMapper2;
import com.learn.project2.model.bo.wx.comment.CommentPostBO2;
import com.learn.project2.model.vo.wx.comment.CommentCountVO2;
import com.learn.project2.model.vo.wx.comment.CommentListVO2;
import com.learn.project2.model.vo.wx.comment.CommentPostVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname CommentServiceImpl2
 * @Description TODO
 * @author王茁
 * @Date 2022/1/10 14:04
 */
@Service
public class CommentServiceImpl2 implements CommentService2 {
    @Autowired
    MarketCommentMapper commentMapper;
    @Autowired
    MarketUserMapper userMapper;

    @Autowired
    CommentMapper2 commentMapperByHand2;

    @Override
    /**
     * @description: TODO 查询(王茁)
     * @author wangzhuo
     * @date 2022-01-10 14:54
     * @version 1.0
     */
    public CommentListVO2 getList(Integer valueId, Byte type, Integer limit, Integer page, Integer showType) {
        PageHelper.startPage(page, limit);
        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andValueIdEqualTo(valueId);
        //评论类型，如果type=0，则是商品评论；如果是type=1，则是专题评论
        criteria.andTypeEqualTo(type);
        //1为只查找有图片的评论
        if (showType == 1) {
            criteria.andHasPictureEqualTo(true);
        }
        //获取 commentList  信息
        //注意：只需查询delted=0的数据
        criteria.andDeletedEqualTo(false);
        List<MarketComment> commentList = commentMapper.selectByExample(commentExample);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(commentList);
        CommentListVO2 commentListVO2 = new CommentListVO2();
        commentListVO2.setTotal((int) pageInfo.getTotal());
        commentListVO2.setPages(pageInfo.getPages());
        commentListVO2.setLimit(limit);
        commentListVO2.setPage(page);

        List<CommentListVO2.ListBean> listBeans = new ArrayList<>();
        List<MarketComment> pageInfoList = pageInfo.getList();
        for (MarketComment marketComment : pageInfoList) {
            CommentListVO2.ListBean listBean = new CommentListVO2.ListBean();
            //userInfo: {"nickName":"userfasdf","avatarUrl":""}
            CommentListVO2.ListBean.UserInfoBean userInfoBean = new CommentListVO2.ListBean.UserInfoBean();
            //通过userId查询market_user表中的nickname和avatar信息
            MarketUser marketUser = userMapper.selectByPrimaryKey(marketComment.getUserId());
            userInfoBean.setNickName(marketUser.getNickname());
            userInfoBean.setAvatarUrl(marketUser.getAvatar());
            listBean.setUserInfo(userInfoBean);
            if (marketComment.getAddTime() != null) {
                listBean.setAddTime(marketComment.getAddTime().toString());
            }
            listBean.setPicList(marketComment.getPicUrls());
            listBean.setAdminContent(marketComment.getAdminContent());
            listBean.setContent(marketComment.getContent());
            listBeans.add(listBean);
        }
        commentListVO2.setList(listBeans);
        return commentListVO2;
    }

    @Override
    /**
     * @description: 获取 总评论数，含图评论数
     * @author wangzhuo
     * @date 2022-01-10 16:03
     * @version 1.0
     */
    public CommentCountVO2 getCount(Integer valueId, Byte type) {
        Integer countHasPic = commentMapperByHand2.getCountHasPic(valueId, type);
        Integer countAll = commentMapperByHand2.getCountAll(valueId, type);
        CommentCountVO2 commentCountVO2 = new CommentCountVO2();
        commentCountVO2.setAllCount(countAll);
        commentCountVO2.setHasPicCount(countHasPic);
        return commentCountVO2;
    }

    @Override
    /**
     * @description: 新建专题评论
     * @author wangzhuo
     * @date 2022-01-10 19:56
     * @version 1.0
     */
    public CommentPostVO2 postSpecialComment(@RequestBody CommentPostBO2 commentPostBO2) {
        MarketComment marketComment = new MarketComment();
        marketComment.setValueId(commentPostBO2.getValueId());
        marketComment.setType(commentPostBO2.getType());
        marketComment.setContent(commentPostBO2.getContent());
        //valueId 就是goodsId
        /**如何获取userId
         *???
         */
        marketComment.setUserId(1);
        marketComment.setHasPicture(commentPostBO2.getHasPicture());
        if(commentPostBO2.getHasPicture()==true) {
            marketComment.setPicUrls(commentPostBO2.getPicUrls());
        }
        marketComment.setStar(commentPostBO2.getStar());
        marketComment.setAddTime(new Date());
        marketComment.setUpdateTime(new Date());
        marketComment.setDeleted(false);
        //添加useGeneratedKeys="true" keyProperty="employee.id"，可返回自增的主键.
        // 返回的值在插入的对象上
        int insert = commentMapper.insert(marketComment);
        CommentPostVO2 commentPostVO2 = new CommentPostVO2();
        commentPostVO2.setId(marketComment.getId());
        commentPostVO2.setValueId(marketComment.getValueId());
        commentPostVO2.setType(marketComment.getType());
        commentPostVO2.setContent(marketComment.getContent());
        commentPostVO2.setUserId(marketComment.getUserId());
        commentPostVO2.setHasPicture(marketComment.getHasPicture());
        commentPostVO2.setPicUrls(marketComment.getPicUrls());
        commentPostVO2.setStar(marketComment.getStar());
        commentPostVO2.setAddTime(marketComment.getAddTime().toString());
        commentPostVO2.setUpdateTime(marketComment.getUpdateTime().toString());
        return commentPostVO2;
    }
}
